{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 提示词重写(仅适用于图像生成)工具测试\n",
    "\n",
    "这个笔记本用于测试 `prompt_utils_openai.py` 中的提示词重写功能。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. 环境设置\n",
    "\n",
    "首先设置必要的环境变量和导入模块。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install openai"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "from pathlib import Path\n",
    "\n",
    "# 添加项目路径\n",
    "sys.path.append('src/examples/tools')\n",
    "\n",
    "# 尝试从 .env 文件加载环境变量\n",
    "def load_env_file():\n",
    "    env_file = Path('.env')\n",
    "    if env_file.exists():\n",
    "        with open(env_file, 'r', encoding='utf-8') as f:\n",
    "            for line in f:\n",
    "                line = line.strip()\n",
    "                if line and not line.startswith('#') and '=' in line:\n",
    "                    key, value = line.split('=', 1)\n",
    "                    key = key.strip()\n",
    "                    value = value.strip().strip('\"').strip(\"'\")\n",
    "                    os.environ[key] = value\n",
    "        print(\"✅ 已从 .env 文件加载环境变量\")\n",
    "        return True\n",
    "    return False\n",
    "\n",
    "# 加载环境变量\n",
    "env_loaded = load_env_file()\n",
    "\n",
    "# 检查API密钥是否设置\n",
    "if 'OPENAI_API_KEY' not in os.environ:\n",
    "    print(\"⚠️ 未找到 OPENAI_API_KEY\")\n",
    "    if not env_loaded:\n",
    "        print(\"💡 请在项目根目录创建 .env 文件，内容如下:\")\n",
    "        print(\"   OPENAI_API_KEY=your-api-key-here\")\n",
    "    else:\n",
    "        print(\"💡 请在 .env 文件中添加 OPENAI_API_KEY=your-api-key-here\")\n",
    "    print(\"   或者设置系统环境变量 OPENAI_API_KEY\")\n",
    "else:\n",
    "    print(\"✅ OPENAI_API_KEY 已设置\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入提示词重写工具\n",
    "try:\n",
    "    from prompt_utils_openai import rewrite, get_caption_language, api\n",
    "    print(\"✅ 成功导入 prompt_utils_openai 模块\")\n",
    "except ImportError as e:\n",
    "    print(f\"❌ 导入失败: {e}\")\n",
    "    print(\"请确保已安装 openai 库: uv add openai\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. 测试语言检测功能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 测试语言检测\n",
    "test_prompts = [\n",
    "    \"一只可爱的小猫\",\n",
    "    \"A cute little cat\",\n",
    "    \"画一个美丽的风景\",\n",
    "    \"Draw a beautiful landscape\",\n",
    "    \"中英混合 mixed language prompt\"\n",
    "]\n",
    "\n",
    "print(\"语言检测测试:\")\n",
    "for prompt in test_prompts:\n",
    "    lang = get_caption_language(prompt)\n",
    "    print(f\"'{prompt}' -> {lang}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. 测试中文提示词重写"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 中文提示词测试用例\n",
    "chinese_prompts = [\n",
    "    \"一只小猫\",\n",
    "    \"美丽的风景\",\n",
    "    \"手机挂绳展示\",\n",
    "    \"古代诗人在月下作诗\",\n",
    "    \"科技感的未来城市\"\n",
    "]\n",
    "\n",
    "print(\"中文提示词重写测试:\")\n",
    "print(\"=\" * 50)\n",
    "\n",
    "for i, prompt in enumerate(chinese_prompts, 1):\n",
    "    print(f\"\\n{i}. 原始提示词: {prompt}\")\n",
    "    try:\n",
    "        rewritten = rewrite(prompt)\n",
    "        print(f\"   重写结果: {rewritten}\")\n",
    "    except Exception as e:\n",
    "        print(f\"   ❌ 重写失败: {e}\")\n",
    "    print(\"-\" * 50)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Qwen-Image (3.12.11)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
